import Vue from "vue";
import VueRouter from "vue-router";
// import Index from "../views/Index.vue";
// import Show from "../test/Show.vue";
Vue.use(VueRouter);

const routes = [
  {
    path:"/myorderdetail/:OrderId?",
    name:"MyOrderDetail",
    component: () => import(/* webpackChunkName : "orderdetail"*/"../views/MyOrderDetail.vue"),
    meta: {
      showHeader: true,
      showFooter: false,
      keepAlive: false,
    },
    props:true,
  },
  {
    path: "/mydetail",
    name: "MyDetail",
    component: () => import(/* webpackChunkName : "mydetail"*/"../views/MyDetail.vue"),
    meta: {
      showHeader: true,
      showFooter: false,
      keepAlive: false,
    },
  },
  {
    path: "/paysuc",
    name: "PaySuc",
    component: () => import(/* webpackChunkName : "paysuc"*/"../views/PaySuc.vue"),
  },
  {
    path: "/ordersuccess/:price?/:orderID?",
    name: "OrderSuccess",
    props: true,
    meta: {
      keepAlive: true,
    },
    component: () => import(/* webpackChunkName : "ordersuc"*/"../views/OrderSuccess.vue"),
  },
  {
    path: "/order/:day?/:location?/:id?/:rentTime?/:price?/:returnTime?",
    name: "Order",
    props: true,
    meta: {
      showHeader: false,
      showFooter: false,
      keepAlive: false,
    },
    component: () => import(/* webpackChunkName : "order"*/"../views/Order.vue"),
  },
  {
    path: "/cartsearch",
    name: "CartSearch",
    meta: {
      showHeader: true,
      showFooter: true,
      keepAlive: false,
    },
    component: () => import(/* webpackChunkName : "carsearch"*/"../views/CartSearch.vue"),
  },
  {
    path: "/",
    meta: {
      showHeader: true,
      showFooter: true,
      keepAlive: false,
    },
    name: "Index",
    component: () => import(/* webpackChunkName : "home"*/"../views/Index.vue"),
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
];
// 默认不写hash  是hash模式，路径会带#
// 打包后 不需要任何特殊处理，就能上线服务
// 路径没有# 上线服务器需要特殊处理
// 未处理 刷新会报错
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 路由前置守卫 判断前端是否携带token进行请求 如果没有携带token 提示先登录
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.name !== "Index" && !token) {
    alert("请先登录!");
    return;
  } else next();
});
export default router;
